#ifndef ADMIN_H
#define	ADMIN_H

using namespace std;

class Admin{
private:
    int id;
    string name;
public:
    Admin(){
        id = 0;
        name = "";
    }
    
    Admin(int userID, string nameInput){
        id = userID;
        name = nameInput;
    }
    
    void createCitizenRec(string userID, string passwd, string name, string date){
        string zSQLstr;
        int userIDInt, dateInt;
        stringstream ss;
        
        Database *db;
        db = new Database("test.db");
        
        userIDInt = atoi(userID.c_str());
        dateInt = atoi(date.c_str());
        
        ss << "insert into user values(" << userIDInt << ", '" << passwd << "','" 
           << name << "'," << date << "," << "'Citizen', 'Inactive');";
        zSQLstr = ss.str();
        
        char* zSQL = (char*)zSQLstr.c_str();
        
        db->query(zSQL);
        
        if(db->getFail() == 1){
            cout << "Error in inputs or existing userid in the system" << endl;
        }else{
            cout << "User " << userIDInt << " is successfully added" << endl;
        }
        
        db->close();
    }
    
    void deleteCitizenRec(string userID){
        string zSQLstr, zSQLstr2, existID;
        int userIDInt;
        stringstream ss, ss2;
        
        Database *db;
        db = new Database("test.db");
        
        userIDInt = atoi(userID.c_str());
        
        ss << "SELECT * FROM user where id = " << userIDInt << ";";
        zSQLstr = ss.str();
        
        char* zSQL = (char*)zSQLstr.c_str();
        
        vector<vector<string> > result = db->query(zSQL);
        
        for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it){
            vector<string> row = *it;
            existID = row.at(0);
        }
        
        if(result.empty()){
            cout << "No such user "<< userID << " in the system." << endl;
        }else{
            ss2 << "delete from user where id = " << userIDInt;
            zSQLstr2 = ss2.str();

            char* zSQL2 = (char*)zSQLstr2.c_str();

            db->query(zSQL2);
            
            cout << "User " << existID << " is successfully deleted" << endl;
        }
        db->close();
    }
    
    void modifyCitizenRec(string userID, int opt, string newInput){
        string zSQLstr, zSQLstr2, existID;
        int userIDInt, dateInt, newUserID;
        stringstream ss, ss2;
        
        Database *db;
        db = new Database("test.db");
        
        userIDInt = atoi(userID.c_str());
        
        ss << "SELECT * FROM user where id = " << userIDInt << ";";
        zSQLstr = ss.str();
        
        char* zSQL = (char*)zSQLstr.c_str();
        
        vector<vector<string> > result = db->query(zSQL);
        
        for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it){
            vector<string> row = *it;
            existID = row.at(0);
        }
        
        if(result.empty()){
            cout << "No such user "<< userID << " in the system.";
        }else{
            switch(opt)
            {
                case 1:
                    ss2 << "update user "
                    << "set id = " << newInput << " "
                    << "where id = " << userIDInt << ";";
                    break;
                case 2:
                    ss2 << "update user "
                    << "set password = '" << newInput << "' "
                    << "where id = " << userIDInt << ";";
                    break;
                case 3:
                    ss2 << "update user "
                    << "set name = '" << newInput << "' "
                    << "where id = " << userIDInt << ";";
                    break;
                case 4:
                    dateInt = atoi(newInput.c_str());
                    ss2 << "update user "
                    << "set date = " << dateInt << " "
                    << "where id = " << userIDInt << ";";
                    break;
                case 5:
                    ss2 << "update user "
                    << "set status = '" << newInput << "' "
                    << "where id = " << userIDInt << ";";
                break;
            }

            zSQLstr2 = ss2.str();

            char* zSQL2 = (char*)zSQLstr2.c_str();

            db->query(zSQL2);
            
            cout << "User " << userID << " is successfully updated" << endl;
        }
        db->close();
    }
};


#endif	/* ADMIN_H */

